package light.head.controller.sys;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.cache.Cache;
import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.sys.OrganTypeDAO;
import light.head.dao.sys.OrganTypeHandleDAO;
import light.head.dao.sys.OrganTypeRelationDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.framework.entity.SystemInfo;
import light.head.model.OrganType;
import light.head.model.OrganTypeHandle;
import light.head.model.OrganTypeRelation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(Url.C_ORGAN_TYPE)
public class OrganTypeController extends SupporterController {
	
	private OrganTypeDAO otDAO;
	private OrganTypeHandleDAO othDAO;
	private OrganTypeRelationDAO otrDAO;
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.ORGAN_TYPE_INIT);
	}

	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME);
		
		if (otDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		
		return new ModelAndView(Url.ORGAN_TYPE_LIST);
	}

	@RequestMapping(Url.DELETE)
	public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.ERROR);
			return;
		}
		
		OrganType ot = otDAO.get(Integer.parseInt(code));
		if (null == ot)
			print(response, Msg.NOT_FIND);
		else {
			if (otDAO.del(ot) == RESULT.SUCCESS)
				print(response, Msg.SUCCESS);
			else 
				print(response, Msg.FAILURE);
		}
	}

	@RequestMapping(Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			OrganType at = otDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.ORGAN_TYPE, at);
			
			if (null != at) {
				List<OrganTypeRelation> sonList = otrDAO.findSonList(at.getCode());
				request.setAttribute(Parm.SON_LIST, sonList);
			}
		}
		
		List<OrganType> list = otDAO.getAll();
		request.setAttribute(Parm.LIST, list);
		
		return new ModelAndView(Url.ORGAN_TYPE_EDIT);
	}
	

	@Transactional
	@RequestMapping(Url.SAVE)
	public void save(OrganType ot, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (null == ot) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}

		RESULT result = RESULT.ERROR;
		if (null == ot.getCode()) {
			ot.setCt(new Date());
			result = otDAO.add(ot);
		} else {
			result = otDAO.upd(ot);
		}
		
		if (RESULT.SUCCESS == result) {
			String[] sonCodes = request.getParameterValues(Parm.CODES);
			List<OrganTypeRelation> oldList = otrDAO.findSonList(ot.getCode());
			
			if (null != sonCodes && sonCodes.length > 0) {
				OrganTypeRelation temp = null;
				int nLen = sonCodes.length;
				int oLen = oldList.size();
				int i = 0;
				for (String nc : sonCodes) {
					for (i = 0; i < oLen; i ++) {
						if (null != nc && nc.equals(oldList.get(i).getScode().toString()))
							break;
					}
					if (i == oLen) {
						temp = new OrganTypeRelation();
						temp.setScode(Integer.parseInt(nc));
						temp.setGfpCode(ot.getCode());
						otrDAO.add(temp);
					}
				}
				
				for (OrganTypeRelation o : oldList) {
					for (i = 0; i < nLen; i ++)
						if (isNotNull(sonCodes[i]) && o.getScode().toString().equals(sonCodes[i]))
							break;
					if (i == nLen)
						otrDAO.del(o);
				}
			} else if (null != oldList && oldList.size() > 0) {
				otrDAO.deleteByParentCode(ot.getCode());
			}
			
			otDAO.loadAll();
			print(response, Msg.SUCCESS);
		} else 
			print(response, Msg.ERROR);
	}

	@RequestMapping(Url.EDIT_HANDLE)
	public ModelAndView editHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
		} else {
			Map<String, Boolean> map = othDAO.getHandleMapByOrganTypeCode(Integer.parseInt(code));
//			List<Module> menu = Cache.moduleList;
			List<SystemInfo> systemInfoList = Cache.systemInfoList;
			request.setAttribute(Parm.HANDLE_MAP, map);
			request.setAttribute(Parm.MENU, systemInfoList);
		}
		request.setAttribute(Parm.CODE, code);
		return new ModelAndView(Url.ORGAN_TYPE_EDIT_HANDLE);
	}

	@RequestMapping(Url.SAVE_HANDLE)
	@Transactional
	public void saveHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		String handleStr = request.getParameter("handleList");
		
		if (null == code || null == handleStr) {
			print(response, Msg.ERROR);
		} else {
			Integer c = Integer.parseInt(code);
			List<OrganTypeHandle> list = othDAO.findByOrganTypeCode(c);
			String[] hs = isNotNull(handleStr) ? handleStr.split(Sys.COMMA) : null;
			
			List<OrganTypeHandle> addList = new ArrayList<OrganTypeHandle>();
			List<OrganTypeHandle> delList = new ArrayList<OrganTypeHandle>();
			
			int i = 0;
			for (OrganTypeHandle ath : list) {
				i = 0;
				for (; null != hs && i < hs.length; i ++)
					if (hs[i].equals(ath.getHc().toString()))
						break;
				
				if (null == hs || hs.length == i)
					delList.add(ath);
			}
			
			if (null != delList && delList.size() > 0)
				othDAO.delAll(delList);
			
			if (null != hs) {
				OrganTypeHandle temp = null;
				for (String h : hs) {
					i = 0;
					for (; i < list.size(); i ++)
						if (h.equals(list.get(i).getHc().toString()))
							break;
					
					if (i == list.size()) {
						temp = new OrganTypeHandle();
						temp.setOtc(c);
						temp.setHc(h);
						addList.add(temp);
					}
				}
				
				if (null != addList && addList.size() > 0)
					othDAO.souAll(addList);
			}
			Cache.organTypeMap.put(c, othDAO.loadHandleMapByOrganTypeCode(c));
			print(response, Msg.SUCCESS);
		}
	}
	
	
	@Autowired
	public void setOtDAO(OrganTypeDAO otDAO) {
		this.otDAO = otDAO;
	}
	@Autowired
	public void setOthDAO(OrganTypeHandleDAO othDAO) {
		this.othDAO = othDAO;
	}
	@Autowired
	public void setOtrDAO(OrganTypeRelationDAO otrd) {
		this.otrDAO = otrd;
	}
	
}
